package chendu.service;

import chendu.dto.OrderDTO;
import chendu.entity.OrderMaster;
import chendu.entity.PhoneInfo;
import chendu.entity.PhoneSpecs;
import chendu.enums.PayStatusEnum;
import chendu.enums.ResultEnum;
import chendu.exception.PhoneException;
import chendu.feign.PhoneAPI;
import chendu.mapper.OrderMasterMapper;
import chendu.util.KeyUtil;
import chendu.vo.OrderDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


@Service
@Slf4j
public class OrderService {

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private PhoneAPI phoneFeignAPI;

    /**
     *查询订单
     */
    public OrderDetailVO findOrderDetailByOrderId(String orderId) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();

        OrderMaster orderMasters = orderMasterMapper.selectById(orderId);

        if(orderMasters == null){
            log.error("【查询订单】订单不存在,orderMaster={}",orderMasters);
            throw new PhoneException(ResultEnum.ORDER_NOT_EXIST);
        }

        BeanUtils.copyProperties(orderMasters,orderDetailVO);
        orderDetailVO.setSpecsPrice(orderMasters.getSpecsPrice().divide(new BigDecimal(100))+".00");

        return orderDetailVO;
    }

    /**
     *新增订单
     */
    public OrderDTO addOrder(OrderDTO orderDTO) {
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO,orderMaster);

        /**
         * 根据订单id查手机规格
         */
        PhoneSpecs phoneSpecs = phoneFeignAPI.findSpecsById(orderDTO.getSpecsId());
        if(phoneSpecs == null){
            log.error("【创建订单】规格不存在,phoneSpecs={}",phoneSpecs);
            throw new PhoneException(ResultEnum.SPECS_NOT_EXIST);
        }
        BeanUtils.copyProperties(phoneSpecs,orderMaster);

        /**
         * 调用phone查询phoneSprcs
         */

        PhoneInfo phoneInfo = phoneFeignAPI.findPhoneById(phoneSpecs.getPhoneId());
        if(phoneInfo == null){
            log.error("【创建订单】手机不存在,phoneInfo={}",phoneInfo);
            throw new PhoneException(ResultEnum.PHONE_NOT_EXIST);
        }
        BeanUtils.copyProperties(phoneInfo,orderMaster);

        //计算总价
        BigDecimal orderAmount = new BigDecimal(0);
        orderAmount = phoneSpecs.getSpecsPrice().divide(new BigDecimal(100))
                .multiply(new BigDecimal(orderDTO.getPhoneQuantity()))
                .add(orderAmount)
                .add(new BigDecimal(10));
        orderMaster.setOrderAmount(orderAmount);

        //orderId
        orderMaster.setOrderId(KeyUtil.createUniqueKey());
        orderDTO.setOrderId(orderMaster.getOrderId());

        //payStatus
        orderMaster.setPayStatus(PayStatusEnum.UNPIAD.getCode());

        orderMasterMapper.insert(orderMaster);
        System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
        /**
         * 调用
         */

        phoneFeignAPI.subStock(orderDTO.getSpecsId(),orderDTO.getPhoneQuantity());

        return orderDTO;
    }


    /**
     * 支付订单
     */
    public String pay(String orderId) {
        OrderMaster orderMaster = orderMasterMapper.selectById(orderId);

        if(orderMaster == null){
            log.error("【支付订单】订单不存在,orderMaster={}",orderMaster);
            throw new PhoneException(ResultEnum.ORDER_NOT_EXIST);
        }

        if(orderMaster.getPayStatus().equals(PayStatusEnum.UNPIAD.getCode())){
            orderMaster.setPayStatus(PayStatusEnum.PAID.getCode());
            orderMasterMapper.updateById(orderMaster);
        } else {
            log.error("【支付订单】订单已支付,orderMaster={}",orderMaster);
        }
        return orderId;

    }
}
